const fs = require('fs');
const path = require('path');
const { MongoClient } = require('mongodb');

// MongoDB connection URL
const url = 'mongodb://localhost:27017';
const dbName = 'rewords';
const collectionName = 'c_bcz_words';

async function importWordsToMongoDB() {
    try {
        // Connect to MongoDB
        const client = await MongoClient.connect(url);
        console.log('Connected to MongoDB');

        const db = client.db(dbName);
        const collection = db.collection(collectionName);

        // Read all JSON files from the words directory
        const wordsDir = path.join(__dirname, 'data', 'words');
        const files = fs.readdirSync(wordsDir);

        // Filter only JSON files
        const jsonFiles = files.filter(file => path.extname(file) === '.json');

        // Process each JSON file
        for (const file of jsonFiles) {
            const filePath = path.join(wordsDir, file);
            const fileContent = fs.readFileSync(filePath, 'utf8');
            const wordData = JSON.parse(fileContent);

            // Add timestamp and filename
            wordData.importedAt = new Date();
            wordData.sourceFile = file;

            // Insert or update the document
            await collection.updateOne(
                { word: wordData.word },
                { $set: wordData },
                { upsert: true }
            );

            console.log(`Imported: ${wordData.word}`);
        }

        console.log('Import completed successfully');
        await client.close();
        console.log('MongoDB connection closed');

    } catch (error) {
        console.error('Error:', error);
        process.exit(1);
    }
}

// Run the import function
importWordsToMongoDB();
